<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/8/14 0014
 * Time: 14:46
 */
namespace app\index\controller;


use app\index\model\Admin;
use think\Controller;
use think\Request;

class Publiccom extends Controller
{
    /**
     * 构造函数
     * @param Request $request Request对象
     * @access public
     */
    public function __construct(Request $request = null)
    {
        parent::__construct();
        session([
            'prefix' => 'module',
            'type' => '',
            'auto_start' => true,
        ]);
        $request= \think\Request::instance();
        $con=$request->controller();
        if ($con!=="Login"){
            if (session('username') == NULL) {
                $this->success('非法操作，请进行登录','index/login/index');
            }
        }
        $map0['username']=session('username');
        $admin = Db('admin')->where($map0)->find();
        $this->assign('admin',$admin);
    }
    public function logins($username,$password)
    {
        $map['username']=$username;
        $admin = Db('admin')->where($map)->find();
        if (!$admin){
            return -2;
        }elseif ($password==$admin['password']){
            session('username',$username);
            session('id',$admin['id']);
            return 1;
        }else{
            return -3;
        }
    }
    //首页系统展示
    public function indexnum()
    {
        $data['php_unnaem']=php_uname();
        $data['php_sapi']=php_sapi_name();
        $data['php_banben']=PHP_VERSION;
        $data['php_os']=PHP_OS;
        $data['admin_num']=count(Db('admin')->select());
        $data['zc_num']=count(Db('users')->select());
        $data['sj_num']=count(Db('users')->where('owner=1')->select());
        $map0['admin_id']=session('admin_id');
        $data['login_num']=count(Db('login')->where($map0)->select());
        $data['notice_num']=count(Db('notice')->where($map0)->select());
        $data['feedback']=count(Db('usersfeedback')->select());
        $loginone=new \app\index\model\Login();
        $login=$loginone->loginone(session('admin_id'));
        $data['login_last']=$login;
        return $data;
    }
    //正则判断是否是手机号
    public function phone($phone)
    {
        if(!preg_match("/^1[34578]{1}\d{9}$/",$phone)) {
            $this->success("手机号格式错误");
        }
    }
    //分页输出数据
    public function pages($model,$map,$pag,$desc)
    {
        $mo = new $model();
        $count = count($mo->where($map)->select());
        $sel = $mo ->where($map)->order("$desc desc")->paginate($pag,$count);
        return $sel;
    }
    public function TanChange($echo,$state)
    {
        if ($state = 0){
            return "<script  charset='UTF-8'>alert('".$echo."');</script>";
        }elseif($state) {
            return "<script  charset='UTF-8'>alert('".$echo."');history.go(0);</script>";
        }
    }
}
